import py_compile
import argparse
import os


def get_source_dest():
    a = argparse.ArgumentParser()
    a.add_argument('--src', default='.')
    a.add_argument('--dst', default='.')

    a = a.parse_args()
    src = a.src
    dst = a.dst
    print('src:', src)
    print('dst:', dst)
    return src, dst


def compile(src, dst):
    src = to_standard_directory(src) + os.sep
    dst = to_standard_directory(dst) + os.sep
    for name in os.listdir(src):
        src_path = src + name
        if os.path.isfile(src_path):
            if name.endswith('.py'):
                os.makedirs(dst, exist_ok=True)
                dst_path = dst + name[:-2] + 'pyc'
                print('compile %s \t==> %s' % (src_path, dst_path), end='')
                try:
                    py_compile.compile(src_path, dst_path)
                    print('\tDone!', end='')
                finally:
                    print()
        else:
            compile(src_path, dst + name)


def to_standard_directory(path: str):
    if path is None:
        return None
    if path[-1] in ('/', '\\'):
        path = path[:-1]
    return path


if __name__ == '__main__':
    src, dst = get_source_dest()
    compile(src, dst)